package com.css.landslide.risk.service.impl;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.css.landslide.risk.entity.GisInfluencelandslide;
import com.css.landslide.risk.mapper.GisInfluencelandslideMapper;
import com.css.landslide.risk.param.LandslideParam;
import com.css.landslide.risk.service.GisInfluencelandslideService;
import org.springframework.stereotype.Service;


@Service
public class GisInfluencelandslideServiceImpl extends ServiceImpl<GisInfluencelandslideMapper, GisInfluencelandslide> implements GisInfluencelandslideService {


    @Override
    public int insertDs(LandslideParam dsEntity, GisInfluencelandslide gisInfluencelandslide) {
        try {
            DynamicDataSourceContextHolder.push(dsEntity.getDataSourceKey());
            return this.baseMapper.insert(gisInfluencelandslide);
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            DynamicDataSourceContextHolder.clear();
        }
    }


    @Override
    public int deleteDs(LandslideParam dsEntity) {
        try {
            DynamicDataSourceContextHolder.push(dsEntity.getDataSourceKey());
            QueryWrapper<GisInfluencelandslide> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("earthquake_id",dsEntity.getEarthQuakeId());
            queryWrapper.eq("influence_version",dsEntity.getInfluenceVersion());
            return this.baseMapper.delete(queryWrapper);
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            DynamicDataSourceContextHolder.clear();
        }
    }
}
